<?php


namespace app\common\model;

use think\facade\Session;

use app\common\model\Common as CommonModel;



class Sms extends CommonModel
{
    protected $pk = 'id';

    /**
     * index页面数据获取
     * */
    public function tableData($post,$field='*',$isPage = true)
    {
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = 10;
        }

        $tableWhere = $this->tableWhere($post);
        $query      = $this
            ->field($field)
            ->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);


        if ($isPage) {
            $list        = $query->paginate([
                'list_rows'=> $limit,//每页数量
                'query' => request()->param(),
            ]);
            $data        = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $page = $list->render();
            $re['count'] = $list->total();
        } else {
            $list        = $query->select();
            $data        = $this->tableFormat($list);         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
            $page = [];
        }

        $re['code'] = 1;
        $re['msg']  = '';
        $re['data'] = $data;
        $re['page'] = $page;
        return $re;
    }


    protected function tableWhere($post)
    {
        $where = $whereOr = [];

        $result['where']   = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";
        $pk = $this->pk;
        $result['order'] = [$pk => 'desc'];
        return $result;
    }

    protected function tableFormat($list)
    {

        return $list;
    }

    /**
     * send_type one=单发，more=群发
    */
    public function send($mobile,$type='common',$send_type="one"){
        $return = [
            'code'=>0,
            'msg'=>'',
            'data'=>[]
        ];
        //注册时已注册过的不予发送
        if($type == 'register'){
            $Users = new Users();
            $one = $Users->where('mobile','=',$mobile)->find();
            if($one['id']){
                $return['msg'] = '此手机号已注册过';
                return $return;
            }
        }

        //登录时 查看用户表中有没有此手机号用户
        if($type == 'login2'){
            $Users = new Users();
            $one = $Users->where('mobile','=',$mobile)->find();
            if(!$one){
                $return['msg'] = '此手机号没有注册过';
                return $return;
            }
        }

        //查询有没有未用并没过期的短信
        $sms_res = $this
            ->where('mobile','=',$mobile)
            ->where('type','=',$type)
            ->where('status','=',0)
            ->where('expire_time','>',time())
            ->select();
        if(count($sms_res) > 0){
            $return['msg'] = '您有短信验证码还未过期，请不要重复提交';
            return $return;
        }
        $rand = rand(1000,9999);
        $content = '【尚层职校】您的验证码为：'.$rand.'，有效期为5分钟，请妥善保存';
        $url = 'http://wdapi.movek.net/smsv2';
        $arr = [
            'account'=>'923437',
            'password'=>'aAtGzs',
            'extno'=>'10690437',
        ];

        if($send_type == 'one'){
            $arr['action'] = 'p2p';
            $arr['mobileContentKvp'] = [$mobile=>$content];
        }
        if($send_type == 'more'){
            $arr['action'] = 'send';
            $arr['mobile'] = $mobile;
            $arr['content'] = $content;
        }
        $header = array(
            'ContentType=application/json;charset=utf-8'
        );
        $res = curlPost($url,$arr,60,$header,'json');
        $res_arr = $result = json_decode($res ,true);
        if($res_arr['status'] != 0){
            $return['msg'] = '发送验证码失败';
            $return['data'] = $res_arr;
            return $return;
        }

        //发送成功 存入数据库
        if($res_arr['status'] == 0){
            $data['mobile'] = $mobile;
            $data['type'] = $type;
            $data['code'] = $rand;
            $data['content'] = $content;
            $data['add_time'] = time();
            $data['expire_time'] = time()+ 5*60;
            $result = $this->save($data);
            if(!$result){
                $return['msg'] = '存入数据库失败';
                return $return;
            }
        }
        $return['code'] = 1;
        $return['msg'] = '发送成功';
        return $return;
    }

    public function checkSmsCode($mobile,$code,$type="common"){
        $return = [
            'code'=>0,
            'msg'=>'',
            'data'=>[]
        ];

        $sms_one = $this
            ->where('type','=',$type)
            ->where('mobile','=',$mobile)
            ->where('status','=',0)
            ->where('expire_time','>',time())
            ->order('id desc')
            ->find();
        if(!$sms_one){
            $return['msg'] = '请重新发送短信验证码';
            return $return;
        }
        if($sms_one['code'] != $code){
            $return['msg'] = '短信验证码错误';
            return $return;
        }

        $res = $this->where('id','=',$sms_one['id'])->update([
            'status'=>1
        ]);
        if(!$res){
            $return['msg'] = '短信验证码使用失败';
            return $return;
        }

        $return['code'] = 1;
        $return['msg'] = '短信验证码验证成功';
        return $return;
    }


}